package net.jumpengine.bean;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import net.jumpengine.po.Border;

public class BoxBase extends Base {
	private Border border;
	private Color back;
	private int r,cx,cy,cwidth,cheight;
	private boolean change=true;

	public BoxBase(int x, int y, int width, int height,Border border,Color back) {
		this(x, y, width, height,0, border, back);
	}
	
	public BoxBase(int x, int y, int width, int height, int index,Border border,Color back) {
		super(x, y, width, height, index);
		this.border=border;
		this.back=back;
	}

	@Override
	public void draw(GraphicsContext g) {
		if(!visible)return;
		r=border==null?0:border.getRadius();
		if(border!=null)g.setFill(border.getColor());
		if(r==0) {
			g.fillRect(x, y, width, height);
		}else {
			g.fillRoundRect(x, y, width, height, r, r);
		}
		
		g.setFill(back);
		
		if(change) {
			cx=x+(border==null?0:border.getLeft());
			cy=y+(border==null?0:border.getTop());
			cwidth= width-(border==null?0:border.getRight())-(border==null?0:border.getLeft());
			cheight=height-(border==null?0:border.getBottom())-(border==null?0:border.getTop());
		}
		
		if(r==0) {
			g.fillRect(cx,cy,cwidth,cheight);
		}else {
			g.fillRoundRect(cx,cy, cwidth, cheight, r, r);
		}
	}
	
	@Override
	public void setHeight(int height) {
		this.height=height;
		change=true;
	}
	@Override
	public void setWidth(int width) {
		this.width=width;
		change=true;
	}
	@Override
	public void setX(int x) {
		this.x=x;
		change=true;
	}
	public void setY(int y) {
		this.y=y;
		change=true;
	}
	public void setBorder(Border border) {
		this.border = border;
		change=true;
	}
}
